import locale from '../locales'

export const autoSaveLangToLocalStorage = (lang) => {
  if (lang) {
    window.localStorage.setItem('tws-lang', lang)
  }
}
export const getLangFromLocalStorage = () => {
  return window.localStorage.getItem('tws-lang') || 'zh'
}

export const convertContent = (content) => {
  const lang = getLangFromLocalStorage()
  return getLangMessage(lang)[content] || content
}

const getLangMessage = (lang) => {
  return locale[lang].intlMessage
}

export const convertDynamicContent = (content) => {
  const neededConvert = content.split(' ')[0]
  return content.replace(neededConvert, convertContent(neededConvert))
}
